/**
 * Created by zhouli on 18/4/25
 * Email li.zhou@huilianyi.com
 */
import React from 'react';
import {connect} from 'react-redux';
import {Table, Icon , message,Popover} from 'antd';
import 'styles/setting/role-setting/receipt-role/receipt-role.scss';
import {messages} from "share/common";
import RSService from 'containers/setting/role-setting/role-setting.service';
import {SelectDepOrPerson} from 'components/index';
import ResizeTable from "components/resize-table";

class RoleSettingReceipt extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: false,
      data: [],
      columns: [
        {
          title: messages('setting.key887'/*姓名*/),
          dataIndex: 'fullName',
        },

        {
          title: messages('setting.key886'/*工号*/),
          dataIndex: 'employeeID',
        },

        {
          title: messages('setting.key888'/*部门*/),
          dataIndex: '_departmentName',
          render: text => <span>{text ? <Popover placement="topLeft" content={text}>{text}</Popover> : '-'}</span>
        },
        {
          title: messages('setting.key959'/*邮箱*/),
          dataIndex: 'email',
        },
        {
          title: messages("common.operation"),//"操作",
          dataIndex: "userOID",
          render: (text, record) => (
            <span>
              <a onClick={(e) => this.removeReceiptUser(e, record)}>
                     {/*删除*/}
                {messages("common.delete")}
              </a>
            </span>
          )
        }
      ],
      pagination: {
        total: 0,
        page: 0,
        pageSize: 10,
      },
    };
  }

  componentDidMount() {
    this.getRoleUserListByRoleName();
  }

  //添加收单人
  addReceiptUser=(arr)=>{
    if(arr.length < 1){
      message.warn(messages('setting.key899'/*请选择人员*/));
      return;
    }
    let userOIDs =[];
    arr.map((item)=>{
      userOIDs.push(item.userOID);
    })

    this.setState({
      loading: true
    })
    let params = {
      authorityName:"ROLE_COMPANY_FINANCE_RECEIVED",
      userOIDs:userOIDs
    }
    RSService.addRoleUser(params)
      .then((res) => {
        this.getRoleUserListByRoleName();
      })
      .catch((res)=>{
        this.setState({
          loading: false
        })
      })

  }
  //设置收单人
  removeReceiptUser=(e, record)=>{
    let params = {
      authorityName:"ROLE_COMPANY_FINANCE_RECEIVED",
      userOIDs:[record.userOID]
    }
    RSService.deleteRoleUser(params)
      .then((res) => {
        this.getRoleUserListByRoleName();
      })
      .catch((res)=>{
        this.setState({
          loading: false
        })
      })
  }
  //分页点击
  onChangePager = (pagination, filters, sorter) => {
    this.setState({
      pagination: {
        page: pagination.current - 1,
        pageSize: pagination.pageSize
      }
    }, () => {
      this.getRoleUserListByRoleName();
    })
  };
  getRoleUserListByRoleName = () => {
    this.setState({
      loading: true
    })
    let pagination = this.state.pagination;
    let params = {
      page: pagination.page,
      size: pagination.pageSize,
    }
    RSService.getRoleUserListByRoleName("ROLE_COMPANY_FINANCE_RECEIVED",params)
      .then((res) => {
        pagination.total = Number(res.headers['x-total-count']) ? Number(res.headers['x-total-count']) : 0;
        pagination.current = params.page + 1;
        res.data.map(item => {
          item._departmentName = item.department && item.department.name ? item.department.name : "";
        })
        this.setState({
          loading: false,
          data: res.data,
          pagination
        })
      })
      .catch((res)=>{
        this.setState({
          loading: false
        })
      })
  }

  //返回
  backToRoleSetting = () => {
    this.context.router.goBack();
  }

  render() {
    const {columns,data,pagination,loading} = this.state;
    return (
      <div className="setting-role-setting-receipt">
        <div className="table-header">
          <div className="table-header-buttons">
            <SelectDepOrPerson
              buttonType={"primary"}
              title={messages("common.add")}
              onlyPerson={true}
              onConfirm={this.addReceiptUser}/>
          </div>
        </div>
        <ResizeTable
          columns={columns}
          dataSource={data}
          pagination={pagination}
          loading={loading}
          bordered
          onChange={this.onChangePager}
          size="middle"
        />
        <a className="back" onClick={this.backToRoleSetting}>
          <Icon type="rollback" style={{marginRight: '5px'}}/>
          {/*返回*/}
          {
            messages("common.back")
          }
        </a>
      </div>
    )
  }
}

RoleSettingReceipt.contextTypes = {
  router: React.PropTypes.object
};

function mapStateToProps() {
  return {}
}

export default connect(mapStateToProps)(RoleSettingReceipt);
